# Maintainer: ImperatorS79 <fevrier.simon@gmail.com>

_realname=gmsh
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=4.11.1
pkgrel=2
pkgdesc="3D finite element mesh generator with built-in CAD engine and post-processor (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32')
url="https://gmsh.info"
license=("spdx:GPL-2.0-or-later")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-make"
             "${MINGW_PACKAGE_PREFIX}-eigen3"
             "${MINGW_PACKAGE_PREFIX}-petsc"
             "${MINGW_PACKAGE_PREFIX}-slepc")
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-cairo"
         "${MINGW_PACKAGE_PREFIX}-fltk"
         "${MINGW_PACKAGE_PREFIX}-gmp"
         "${MINGW_PACKAGE_PREFIX}-libjpeg"
         "${MINGW_PACKAGE_PREFIX}-libpng"
         "${MINGW_PACKAGE_PREFIX}-med"
         "${MINGW_PACKAGE_PREFIX}-opencascade"
         "${MINGW_PACKAGE_PREFIX}-zlib")
source=(https://gmsh.info/src/gmsh-${pkgver}-source.tgz
        0001-fix-link-lib-name.patch
        0002-fix-dynamic-linking.patch)
sha256sums=('c5fe1b7cbd403888a814929f2fd0f5d69e27600222a18c786db5b76e8005b365'
            'e575ae8fc757694affb6749dc9d52c336691b06159845655ef2ac27aebae8159'
            '4d3f5c4190529e3d45b786adab83f0d65e726357d7367f682ff95b733a88a8c3')

apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying ${_patch}"
    patch -Nbp1 -i "${srcdir}/${_patch}"
  done
}

prepare() {
  cd "${srcdir}/gmsh-${pkgver}-source"

  apply_patch_with_msg \
    0001-fix-link-lib-name.patch \
    0002-fix-dynamic-linking.patch
}

build() {
  [[ -d "${srcdir}/build-${MSYSTEM}" ]] && rm -rf "${srcdir}/build-${MSYSTEM}"
  mkdir "${srcdir}/build-${MSYSTEM}" && cd "${srcdir}/build-${MSYSTEM}"

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    "${MINGW_PREFIX}"/bin/cmake \
      -GNinja \
      -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
      -DCMAKE_BUILD_TYPE=Release \
      -DENABLE_BUILD_DYNAMIC=1 \
      -DENABLE_BUILD_SHARED=1 \
      -DENABLE_OCC=1 \
      -DENABLE_OPENMP=1 \
      -DENABLE_OPENACC=1 \
      -DENABLE_PETSC=1 \
      -DENABLE_SLEPC=1 \
      -DGMSH_RELEASE=1 \
      -DENABLE_CAIRO=1 \
      ../gmsh-${pkgver}-source

  "${MINGW_PREFIX}"/bin/cmake --build .
}

check () {
  cd "${srcdir}/build-${MSYSTEM}"
  "${MINGW_PREFIX}"/bin/ctest .
}

package() {
  cd "${srcdir}/build-${MSYSTEM}"
  DESTDIR="${pkgdir}" "${MINGW_PREFIX}"/bin/cmake --install .

  mv "${pkgdir}${MINGW_PREFIX}"/lib/*.dll "${pkgdir}${MINGW_PREFIX}"/bin/
}
